﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using LoghogInterface.Models;
using LoghogInterface.ViewModels;

namespace LoghogInterface.Modules
{
    /// <summary>
    /// Класс для работы с таблицей пользователей
    /// </summary>
    class SettingsUserRightsSQLHelper
    {
        /// <summary>
        /// Схема базы данных
        /// </summary>
        private DBSchemeDataContextDataContext _database;

        public SettingsUserRightsSQLHelper()
        {
            _database = new DBSchemeDataContextDataContext(SQLQueries.ConnectionString());
            _database.Connection.Open();
        }

        /// <summary>
        /// Получить группы пользователей
        /// </summary>
        /// <returns>Возвращает ObservableCollection<ViewModelUpdateDataUsersRights> с группами</returns>
        public ObservableCollection<ViewModelUpdateDataUsersRights> GetUserRights()
        {
            var users = (from groups in _database.Groups
                         select new ViewModelUpdateDataUsersRights()
                         {
                             ID = groups.ID,
                             Name = groups.Name.ToString(),
                             Read = groups.CanRead.Value,
                             Add = groups.CanAdd.Value,
                             Export = groups.CanExport.Value,
                             Import = groups.CanImport.Value,
                             AddField = groups.CanAddField.Value,
                             RemoveField = groups.CanDelField.Value,
                             AddUser = groups.CanAddUsers.Value,
                             RemoveUser = groups.CanDelUsers.Value,
                             CreateGroup = groups.CanCreateGroups.Value,
                             RemoveGroup = groups.CanDelGroups.Value,
                             CreateJournal = groups.CanCreateJournals.Value,
                             RemoveJournal = groups.CanDelJournals.Value
                         });

            ObservableCollection<ViewModelUpdateDataUsersRights> result = new ObservableCollection<ViewModelUpdateDataUsersRights>(users);
            return result;
        }

        /// <summary>
        /// Обновление данных о группах.
        /// </summary>
        /// <param name="tableRights">Структура с данными групп</param>
        public void UpdateUserRights(ObservableCollection<ViewModelUpdateDataUsersRights> tableRights)
        {
            foreach (var rowRights in tableRights)
            {
                var rights = (from right in _database.Groups 
                              where right.ID == rowRights.ID
                              select right);

                foreach (var right in rights)
                {
                    right.Name = rowRights.Name;
                    right.CanRead = rowRights.Read; 
                    right.CanAdd = rowRights.Add;
                    right.CanExport = rowRights.Export;
                    right.CanImport = rowRights.Import;
                    right.CanAddField = rowRights.AddField;
                    right.CanDelField = rowRights.RemoveField;
                    right.CanAddUsers = rowRights.AddUser;
                    right.CanDelUsers = rowRights.RemoveUser;
                    right.CanCreateGroups = rowRights.CreateGroup;
                    right.CanDelGroups = rowRights.RemoveGroup;
                    right.CanCreateJournals = rowRights.CreateJournal;
                    right.CanDelJournals = rowRights.RemoveJournal;
                }
                _database.SubmitChanges();
            }
        }
    }
}
